{
 "cells": [
  {
   "cell_type": "code",
   "id": "initial_id",
   "metadata": {
    "collapsed": true,
    "ExecuteTime": {
     "end_time": "2025-11-06T02:45:53.648469Z",
     "start_time": "2025-11-06T02:45:53.643657Z"
    }
   },
   "source": [
    "import numpy as np\n",
    "# 导入归一化类\n",
    "from sklearn.preprocessing import MinMaxScaler"
   ],
   "outputs": [],
   "execution_count": 47
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-11-06T02:45:53.675099Z",
     "start_time": "2025-11-06T02:45:53.670013Z"
    }
   },
   "cell_type": "code",
   "source": "X = [[2, 1], [3, 1], [1, 4], [2, 6]]",
   "id": "bb7bb9a6da0bdd18",
   "outputs": [],
   "execution_count": 48
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-11-06T02:45:53.716427Z",
     "start_time": "2025-11-06T02:45:53.710198Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 定义归一化类的对象\n",
    "scaler = MinMaxScaler(feature_range=(-1, 1))\n",
    "# 将缩放器运用到特征上\n",
    "X_scaled = scaler.fit_transform(X)\n",
    "print(X_scaled)"
   ],
   "id": "a9b100bb7032879b",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 0.  -1. ]\n",
      " [ 1.  -1. ]\n",
      " [-1.   0.2]\n",
      " [ 0.   1. ]]\n"
     ]
    }
   ],
   "execution_count": 49
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-11-06T02:45:53.946915Z",
     "start_time": "2025-11-06T02:45:53.937725Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 导入标准化类\n",
    "from sklearn.preprocessing import StandardScaler"
   ],
   "id": "a4ab144f4dea032a",
   "outputs": [],
   "execution_count": 50
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-11-06T02:45:53.991007Z",
     "start_time": "2025-11-06T02:45:53.986518Z"
    }
   },
   "cell_type": "code",
   "source": "print(X)",
   "id": "8e6f9a1b94acc79d",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[2, 1], [3, 1], [1, 4], [2, 6]]\n"
     ]
    }
   ],
   "execution_count": 51
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-11-06T02:45:54.038474Z",
     "start_time": "2025-11-06T02:45:54.030635Z"
    }
   },
   "cell_type": "code",
   "source": [
    "scaler = StandardScaler()\n",
    "X_scaled = scaler.fit_transform(X)\n",
    "print(X_scaled)"
   ],
   "id": "c19bf95f5a9d70b9",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 0.         -0.94280904]\n",
      " [ 1.41421356 -0.94280904]\n",
      " [-1.41421356  0.47140452]\n",
      " [ 0.          1.41421356]]\n"
     ]
    }
   ],
   "execution_count": 52
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-11-06T02:45:54.058204Z",
     "start_time": "2025-11-06T02:45:54.052697Z"
    }
   },
   "cell_type": "code",
   "source": [
    "X = np.array(X)\n",
    "print(X)"
   ],
   "id": "2a3b5d4b8299d64",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[2 1]\n",
      " [3 1]\n",
      " [1 4]\n",
      " [2 6]]\n"
     ]
    }
   ],
   "execution_count": 53
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-11-06T02:45:54.079316Z",
     "start_time": "2025-11-06T02:45:54.073989Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 平均值\n",
    "mean = X.mean(axis=0)\n",
    "print(mean)"
   ],
   "id": "88b84a83b5fc6b2",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[2. 3.]\n"
     ]
    }
   ],
   "execution_count": 54
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-11-06T02:45:54.106990Z",
     "start_time": "2025-11-06T02:45:54.102557Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 标准差\n",
    "std = X.std(axis=0)\n",
    "print(std)"
   ],
   "id": "52b7875bef574d39",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[0.70710678 2.12132034]\n"
     ]
    }
   ],
   "execution_count": 55
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-11-06T02:45:54.123004Z",
     "start_time": "2025-11-06T02:45:54.118733Z"
    }
   },
   "cell_type": "code",
   "source": "print(X - mean / std)",
   "id": "ed678c5f9d51b9e4",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[-0.82842712 -0.41421356]\n",
      " [ 0.17157288 -0.41421356]\n",
      " [-1.82842712  2.58578644]\n",
      " [-0.82842712  4.58578644]]\n"
     ]
    }
   ],
   "execution_count": 56
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
